package com.winhxd.b2c.system.controller;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.system.org.condition.SysOrgCondition;
import com.winhxd.b2c.common.domain.system.user.model.SysOrg;
import com.winhxd.b2c.common.feign.system.OrgServiceClient;
import com.winhxd.b2c.system.service.SysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门
 * @author shumin
 * @create 2019/1/7 0007 13:56
 */
@Api(tags = "系统部门管理")
@RestController
@RequestMapping("/")
public class SysOrgController implements OrgServiceClient {

    private static final Logger logger = LoggerFactory.getLogger(SysOrgController.class);

    private static final String MODULE_NAME = "系统部门管理";

    @Autowired
    private SysOrgService sysOrgService;

    /* *
     *查询部门列表列表
     * @author shumin
     * @date  2019/1/7 0007 14:34
    */
    @Override
    @ApiOperation(value = "查询部门列表列表", response = SysOrg.class)
    public ResponseResult<PagedList<SysOrg>> findOrgPageList(@RequestBody SysOrgCondition condition) {
        logger.info("{} - 查询部门列表列表, 参数：condition={}", MODULE_NAME, condition);
        ResponseResult<PagedList<SysOrg>> result = new ResponseResult<>(BusinessCode.CODE_OK);
        PagedList<SysOrg> page = sysOrgService.findOrgPageList(condition);
        result.setData(page);
        return result;
    }

    /* *
     *新增部门
     * @author shumin
     * @date  2019/1/7 0007 15:50
    */
    @Override
    @ApiOperation(value = "新增部门")
    public ResponseResult<Void> saveOrg(@RequestBody SysOrg sysOrg) {
        logger.info("{} - 新增部门, 参数：sysOrg={}", MODULE_NAME, sysOrg);
        sysOrgService.saveOrg(sysOrg);
        return new ResponseResult<>();
    }
    /* *
     *修改部门
     * @author shumin
     * @date  2019/1/7 0007 15:57
    */
    @Override
    @ApiOperation(value = "修改部门")
    public ResponseResult<Void> editOrg(@RequestBody SysOrg sysOrg) {
        logger.info("{} - 修改部门, 参数：sysOrg={}", MODULE_NAME, sysOrg);
        sysOrgService.editOrg(sysOrg);
        return new ResponseResult<>();
    }

    /* *
     * 根据id删除部门
     * @author shumin
     * @date  2019/1/7 0007 16:00
    */
    @Override
    @ApiOperation(value = "删除部门")
    public ResponseResult<Void> deleteOrg(@RequestBody SysOrg sysOrg) {
        logger.info("{} - 删除部门, 参数：id={}", MODULE_NAME, sysOrg.getId());
        sysOrgService.deleteOrg(sysOrg);
        return new ResponseResult<>();
    }

    @Override
    @ApiOperation(value = "根据id获取一个部门")
    public ResponseResult<SysOrg> getById(@PathVariable("id") Long id) {
        logger.info("{} - 根据id查询部门, 参数：id={}", MODULE_NAME, id);
        ResponseResult<SysOrg> result = new ResponseResult<>();
        result.setData(sysOrgService.getById(id));
        return result;
    }

    @Override
    public ResponseResult<List<SysOrg>> findOrgList(@RequestBody SysOrgCondition condition) {
        ResponseResult<List<SysOrg>> result = new ResponseResult<>();
        result.setData(sysOrgService.findOrgList(condition));
        return result;
    }
}
